package com.miao.api;

import com.miao.entity.Demo;
import com.miao.enums.REnum;
import com.miao.exception.BusinessException;
import com.miao.service.IDemoService;
import com.miao.util.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author miao
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/demoApi")
//@CrossOrigin        //允许跨域，5173->8080端口跨域
public class DemoController {

    @Autowired
    private IDemoService demoService;
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    //rest 风格
    @PostMapping("/demo")
    public JSONResponse demo(@RequestBody Demo  demo){
        demoService.save(demo);
        return JSONResponse.ok();
    }

    @GetMapping("/demo/{id}")   //   localhost:8080/miao-vedio-server/demoApi/demo/1926800509890150402
    public JSONResponse getDemoById(@PathVariable Long id){
        Demo demo = demoService.getById(id);
        return JSONResponse.ok(demo);
    }

    @GetMapping("/login")   //   /demo/1926800509890150402
    public JSONResponse login(String username, String password) throws  BusinessException{

        //判断密码，如果没匹配上，if（）
//        throw new BusinessException(
//                BusinessExceptionEnum.USER_NOT_EXIST.getCode(),
//                BusinessExceptionEnum.USER_NOT_EXIST.getMsg());
        return JSONResponse.error(REnum.USER_NOT_EXIST.getCode(), REnum.USER_NOT_EXIST.getMsg());


//        Demo demo = demoService.getById(id);
//        return JSONResponse.ok();
    }
}
